package b.gate.utils.conf

import com.typesafe.config.{Config, ConfigObject}

import scala.collection.JavaConversions._

/**
 * 贲国称 创建于 16/1/8。
 */
trait ConfOp {
  protected def valueGet[T](c: Config, key: String, defaultValue: Option[T] = None): T = {
    val exists = c.hasPath(key)
    if (exists) c.getAnyRef(key).asInstanceOf[T]
    else if (defaultValue.isDefined) defaultValue.get
    else throw new AssertionError(s"配置[${c.origin().filename()}]的配置项[$key]是必需的")
  }
  protected def objectGet(c: Config, key: String, required: Boolean = true): Config = {
    val exists = c.hasPath(key)
    if (exists) c.getObject(key).toConfig
    else if (!required) null
    else throw new AssertionError(s"配置[${c.origin().filename()}]的配置项[$key]是必需的")
  }
  protected def objectsGet(c: Config, key: String, required: Boolean = true): List[ConfigObject] = {
    val exists = c.hasPath(key)
    if (exists) c.getObjectList(key).toList
    else if (!required) List.empty[ConfigObject]
    else throw new AssertionError(s"配置[${c.origin().filename()}]的配置项[$key]是必需的")
  }
  protected def stringsGet(c: Config, key: String, required: Boolean = true): List[String] = {
    val exists = c.hasPath(key)
    if (exists) c.getStringList(key).toList
    else if (!required) List.empty[String]
    else throw new AssertionError(s"配置[${c.origin().filename()}]的配置项[$key]是必需的")
  }
}
